<?php
include('connect_db.php');
/*if logged in, redirect to homepage */
if (!checkLoggedin())
{
	header('Location: login.php');
}

if(ISSET($_GET) && ISSET($_GET['id']))
{
	$other_user_id = $_GET['id'];

	/* I paid */
    $query = 'SELECT * FROM bills B 
    		  	JOIN bills_users BU ON B.id = BU.bill_id
    		  	WHERE B.user_id = '.$_SESSION['id'].
    		  	' AND BU.user_id = '.$other_user_id.
    		  	' AND BU.if_paid != True';
	$results = mysql_query($query);


    while($row = mysql_fetch_array($results))
    {
	    $query = "UPDATE bills_users
					SET if_paid = True
					WHERE user_id = ".$other_user_id.
					" AND bill_id = ".$row['bill_id'];
		mysql_query($query);
    }

    /* I own */
    $query = 'SELECT * FROM bills B 
    		  	JOIN bills_users BU ON B.id = BU.bill_id
    		  	WHERE B.user_id = '.$other_user_id.
    		  	' AND BU.user_id = '.$_SESSION['id'].
    		  	' AND BU.if_paid != True';
	$results = mysql_query($query);

    while($row = mysql_fetch_array($results))
    {
	    $query = "UPDATE bills_users
						SET if_paid = True
						WHERE user_id = ".$_SESSION['id'].
						" AND bill_id = ".$row['bill_id'];
		mysql_query($query);
    }
}

header('Location: index.php');
